//
//  MKVisitorView.swift
//  Weibo
//
//  Created by mac on 15/12/13.
//  Copyright © 2015年 MK. All rights reserved.
//

import UIKit

/*
1. 声明协议 和 方法
2. 设置代理属性 __weak ... id<Delegate>
3. 让代理响应协议方法

4. 遵守协议的代理
5. 实现协议方法
*/

//声明协议 和方法
@objc protocol MKVisitorViewDelegate : NSObjectProtocol {
    //可选 @objc添加到 声明协议的头部
    optional func willLoginClick()
    //必须实现
    func willRegisterClick()
    
}

class MKVisitorView: UIView {
    
    //设置代理
    weak var visitorDelegate: MKVisitorViewDelegate?
    
    //向外提供换图换文字的方法
    func setupInfo(labelText: String, imaName: String?/*?可选 让首页为nil 其他分别替换图片*/) {
        textLabel.text = labelText
        //如果 imaName有值 就不让其为首页 进行换图换文字 /如果imaName没值 让首页直接做旋转的动画就OK了
        if let imNamed = imaName {
            circleView.image = UIImage(named: imNamed)
            bringSubviewToFront(circleView)
            houseImage.hidden = true
        } else {
            didAnimation()
        }
    }
    
    //首页的动画
    private func didAnimation() {
        //创建基本动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画属性
        animation.repeatCount = MAXFLOAT
        animation.duration = 10.0
        animation.toValue = 2 * M_PI
        
        //MARK: - 让其一直转动
        animation.removedOnCompletion = false
        
        //开启动画
        circleView.layer.addAnimation(animation, forKey: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //添加子视图
        addSubview(circleView)
        addSubview(backView)
        addSubview(houseImage)
        addSubview(textLabel)
        addSubview(leftBtn)
        addSubview(rightBtn)
        //自动布局所有的子控件
        layoutAllLazySubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //布局所有的子视图
    func layoutAllLazySubviews() {
        
        //既然选择了手码布局 自动布局最好取消了
        for subview in subviews {
            subview.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //房子的x/y ＝ self.view的x/y
        addConstraint(NSLayoutConstraint(item: houseImage, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseImage, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -50))
        
        //转动的圈圈的x/y = 房子的x/y
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: houseImage, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: houseImage, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //文本的x/顶 = 转动的圈圈的x/底 + 偏移量
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        //toItem: nil, attribute: .NotAnAttribute
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 44))
        
        //左边 登录按钮
        addConstraint(NSLayoutConstraint(item: leftBtn, attribute: .Left, relatedBy: .Equal, toItem: textLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: leftBtn, attribute: .Top, relatedBy: .Equal, toItem: textLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: leftBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: leftBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 34
            ))
        
        //右边 注册按钮
        addConstraint(NSLayoutConstraint(item: rightBtn, attribute: .Right, relatedBy: .Equal, toItem: textLabel, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: rightBtn, attribute: .Top, relatedBy: .Equal, toItem: textLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: rightBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: rightBtn, attribute: .Height
            , relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 34))
        
        //左右按钮的监听
        leftBtn.addTarget(self, action: "loginClick", forControlEvents: .TouchUpInside)
        rightBtn.addTarget(self, action: "registerClick", forControlEvents: .TouchUpInside)
        
        
        //MARK: - 遮罩层 －VFL语言
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView" : backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-35)-[rightBtn]", options: [], metrics: nil, views: ["backView" : backView, "rightBtn" : rightBtn]))
        
        //背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
    }
    
    
    //按钮监听方法的实现
    @objc private func loginClick() {
        //可选的方法 需要?
        //print("loginClick")
        visitorDelegate?.willLoginClick?()
    }
    
    @objc private func registerClick() {
        //print("registerClick")
        visitorDelegate?.willRegisterClick()
    }
    
    
    
    //懒加载所有的子视图
    private lazy var houseImage: UIImageView  = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var textLabel: UILabel = UILabel(tText: "关注一些人，回这里看看有什么惊喜,关注一些人，回这里看看有什么惊喜", tColor: UIColor.darkGrayColor(), tFont: 14)
    
    private lazy var leftBtn: UIButton = UIButton(imageName: "common_button_white_disable", titleColor: UIColor.darkGrayColor(), titleName: "登录", titleFont: 14, isnothighLight: false)
 
    
    private lazy var rightBtn: UIButton = UIButton(imageName: "common_button_white_disable", titleColor: themeColor, titleName: "注册", titleFont: 14, isnothighLight: false)
    

}

